singularity

High-and-low level mental-models


let mut x;

// 1. this access would be illegal, nowhere to draw the flow from:
// assert_eq!(x, 42);
x = 42;

// 2. this is okay, can draw a flow from the value assigned above:
let y = &x;

// 3. this establishes a second, mutable flow from x:
x = 43;

// 4. this continues the flow from y, which in turn draws from x.
// but that flow conflicts with the assignment to x!
assert_eq!(*y, 42);

Listing 1-2

error[E0506]: cannot assign to `x` because it is borrowed
  --> src/main.rs:13:5
   |
10 |     let y = &x;
   |             -- `x` is borrowed here
...
13 |     x = 43;
   |     ^^^^^^ `x` is assigned to here but it was already borrowed
...
17 |     assert_eq!(*y, 42);
   |     ------------------ borrow later used here